<template>
  <upload-excel-component :on-success="handleSuccess" :before-upload="beforeUpload" />
</template>

<script>
import UploadExcelComponent from '@/components/UploadExcel/index.vue'
import { formatExcelDate } from '@/utils/handlerData'
import { importEmployee } from '@/api/commpany'
export default {
  name: 'ImportExcle',
  props: {},
  components: {
    UploadExcelComponent
  },
  data () {
    return {
      obj: {
        入职日期: 'timeOfEntry',
        姓名: 'username',
        工号: 'workNumber',
        手机号: 'mobile',
        转正日期: 'correctionTime'
      },
      newArr: []
    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {
    handleSuccess ({ header, results }) {
      // console.log(header, results) // 表头 和表格的内容
      // 转换成后台需要的数据，发送请求
      this.formateDate(results)
    },
    beforeUpload (file) {
      const isLt1M = file.size / 1024 / 1024 < 2
      if (isLt1M) {
        return true
      }
      this.$message({
        message: '文件大小大于2M',
        type: 'warning'
      })
      return false
    },
    async  formateDate (results) {
      results.forEach((item, index) => {
        const newObj = {}
        for (const k in this.obj) {
          if (k === '入职日期' || k === '转正日期') {
            item[k] = formatExcelDate(item[k])
          }
          newObj[this.obj[k]] = item[k]
        }
        this.newArr.push(newObj)
      })
      await importEmployee(this.newArr)
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
